package com.zkingsoft.actions.admin.web;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.model.web.WebAd;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.web.WebAdService;
import com.zkingsoft.services.web.WebAdcolumnService;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-12-20 15:50
 */
@Controller
@RequestMapping(value = "admin/webAd")
public class WebAdController extends BaseController {

	@Resource
	private WebAdService webAdService;

	@Resource
	private WebAdcolumnService webAdcolumnService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "WebAd_BEV";

	public static final String fnCode = "WebAd";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebAd webAd, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		SysUsers user = matrixAdapter.getLoginUser();
		webAd.setCompanyId(user.getCompanyId());
		return showList(webAdService, webAd, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebAd webAd) {
		if (webAd.getAdId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			AjaxResult result = modify(webAdService, WebUtil.getSessionAttribute(this.BEV), webAd, "广告");
			WebUtil.removeSessionAttribute(this.BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			if (webAd.getAdTime() == null) {
				webAd.setAdTime(new Date());
			}
			SysUsers user = matrixAdapter.getLoginUser();
			webAd.setCompanyId(user.getCompanyId());
			return add(webAdService, webAd, "广告");
		}
	}

	/**
	 * 进入修改界面
	 */
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		WebAd webAd;
		if (id != null) {
			webAd = webAdService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webAd);
			WebUtil.setSessionAttribute(this.BEV, webAd);
		}
		return "admin/web/ad-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(webAdService, keys);
	}

}
